package product

import (
	"vue-admin-server/global"
	"vue-admin-server/model/product"
	"vue-admin-server/model/product/response"
)

type ReliabilityReportService struct{}

func (trs *ReliabilityReportService) GetReliabilityReportHistory(chipIdStr string) (reports []response.ReliabilityReportResp, err error) {
	var reliabilityReports []product.ReliabilityReport
	err = global.GVA_DB.Where("chip_id = ?", chipIdStr).Order("test_time desc").Find(&reliabilityReports).Error
	if err != nil {
		return nil, err
	}

	for _, r := range reliabilityReports {
		reports = append(reports, response.ReliabilityReportResp{
			GVA_MODEL: global.GVA_MODEL{
				ID:        r.ID,
				CreatedAt: r.CreatedAt,
				UpdatedAt: r.UpdatedAt,
			},
			TestTime: r.TestTime,
		})
	}
	return
}

func (trs *ReliabilityReportService) GetReliabilityReportInfo(chipIdStr, reportIdStr string) (*response.ChipReliabilityReport, error) {
	var chipSpec product.ChipSpec
	err := global.GVA_DB.First(&chipSpec, "id = ?", chipIdStr).Error
	if err != nil {
		return nil, err
	}

	var reliabilityReport product.ReliabilityReport
	err = global.GVA_DB.First(&reliabilityReport, "id = ?", reportIdStr).Error
	if err != nil {
		return nil, err
	}

	reliabilityReportResp := &response.ChipReliabilityReport{
		ChipID:                          chipSpec.ID,
		Supplier:                        chipSpec.Supplier,
		ChipNo:                          chipSpec.ChipNo,
		RaycusNo:                        chipSpec.RaycusNo,
		ModuleType:                      reliabilityReport.ModuleType,
		TestTime:                        reliabilityReport.TestTime,
		TestContent:                     reliabilityReport.TestContent,
		ReportFileURL:                   reliabilityReport.ReportFileURL,
		RatedCurrent:                    reliabilityReport.RatedCurrent,
		RatedPower:                      reliabilityReport.RatedPower,
		RatedJunctionTemperature:        reliabilityReport.RatedJunctionTemperature,
		RatedJunctionTemperatureKelvin:  reliabilityReport.RatedJunctionTemperatureKelvin,
		BurnInCurrent:                   reliabilityReport.BurnInCurrent,
		BurnInPower:                     reliabilityReport.BurnInPower,
		BurnInJunctionTemperature:       reliabilityReport.BurnInJunctionTemperature,
		BurnInJunctionTemperatureKelvin: reliabilityReport.BurnInJunctionTemperatureKelvin,
		ActivationEnergy:                reliabilityReport.ActivationEnergy,
		XFactor:                         reliabilityReport.XFactor,
		YFactor:                         reliabilityReport.YFactor,
		BoltzmannConstant:               reliabilityReport.BoltzmannConstant,
		CurrentAccelerationFactor:       reliabilityReport.CurrentAccelerationFactor,
		PowerAccelerationFactor:         reliabilityReport.PowerAccelerationFactor,
		TemperatureAccelerationFactor:   reliabilityReport.TemperatureAccelerationFactor,
		TotalAccelerationFactor:         reliabilityReport.TotalAccelerationFactor,
		ConfidenceCoefficient:           reliabilityReport.ConfidenceCoefficient,
		InvalidNumber:                   reliabilityReport.InvalidNumber,
		ChiSquare:                       reliabilityReport.ChiSquare,
		TotalTestTime:                   reliabilityReport.TotalTestTime,
		FailureRate:                     reliabilityReport.FailureRate,
		MeanTimeBetweenFailure:          reliabilityReport.MeanTimeBetweenFailure,
		AccelerationFactor:              reliabilityReport.AccelerationFactor,
		RatedFailureRate:                reliabilityReport.RatedFailureRate,
		AcceleratedFailureRate:          reliabilityReport.AcceleratedFailureRate,
		Params:                          make([]response.ReliabilityData, 0),
	}

	// data
	var datas []product.ReliabilityData
	err = global.GVA_DB.Where("report_id = ?", reportIdStr).Order("id").Find(&datas).Error
	if err != nil {
		return reliabilityReportResp, err
	}

	// week data
	for _, d := range datas {
		data := response.ReliabilityData{
			ID:            d.ID,
			ReportID:      d.ReportID,
			Position:      d.Position,
			Sn:            d.Sn,
			Project:       d.Project,
			ModuleType:    reliabilityReport.ModuleType,
			AgingCurrent:  d.AgingCurrent,
			State:         d.State,
			FinalPower:    d.FinalPower,
			Af:            d.Af,
			TotalTestTime: d.TotalTestTime,
			WeekDatas:     make([]product.ReliabilityWeekData, 0),
		}

		err = global.GVA_DB.Where("data_id = ?", d.ID).Order("week").Find(&data.WeekDatas).Error
		if err != nil {
			return reliabilityReportResp, err
		}

		reliabilityReportResp.Params = append(reliabilityReportResp.Params, data)
	}
	return reliabilityReportResp, nil
}
